import { generatedRoutes } from '@/router/elegant/routes';
import type { ElegantConstRoute } from '@elegant-router/types';

/** 常量路由，不需要验证权限的路由 */
const constantRoutes: ElegantConstRoute[] = generatedRoutes.filter(route => route.meta?.constant);

/** 获取常量路由 */
export function getConstantRoutes() {
  return {
    code: '0000',
    data: constantRoutes,
    msg: '获取常量路由成功'
  };
}

/** 用户权限路由，需要根据用户角色过滤的路由 */
const authRoutes: ElegantConstRoute[] = generatedRoutes.filter(route => !route.meta?.constant);

/** 获取用户路由 */
export function getUserRoutes() {
  // 这里可以根据用户角色返回不同的路由
  const routes = authRoutes.map(route => ({
    ...route,
    id: `${Math.random().toString(36).substring(2, 10)}`
  }));

  return {
    code: '0000',
    data: {
      routes,
      home: 'home'
    },
    msg: '获取用户路由成功'
  };
}

/** 判断路由是否存在 */
export function isRouteExist(routeName: string) {
  const isExist = generatedRoutes.some(route => route.name === routeName);

  return {
    code: '0000',
    data: isExist,
    msg: `路由${isExist ? '存在' : '不存在'}`
  };
} 